﻿#include "GY_PlayBackControlWidget.h"
#include "ui_GY_PlayBackControlWidget.h"
#include <QDateTime>
/**
 * @brief microsecondToString 微妙转换字符串
 * @param microsecond 微妙数
 * @return 时间字符串
 */
QString microsecondToString(const quint64& microsecond)
{
    quint64 timeCount=microsecond;
    QString timeString=QString("%1").arg(timeCount/1000/1000/60/60,2,10,QLatin1Char('0'));
    timeCount=timeCount%((quint64)1000*1000*60*60);
    timeString+=QString(":%1").arg(timeCount/1000/1000/60,2,10,QLatin1Char('0'));
    timeCount=timeCount%((quint64)1000*1000*60);
    timeString+=QString(":%1").arg(timeCount/1000/1000,2,10,QLatin1Char('0'));
    timeCount=timeCount%((quint64)1000*1000);
    timeString+=QString(".%1").arg(timeCount/1000,3,10,QLatin1Char('0'));
    timeCount=timeCount%((quint64)1000);
     timeString+=QString(".%1").arg(timeCount,3,10,QLatin1Char('0'));
    return timeString;
}
/**
 * @brief microsecondToTime 微妙转换成QTime
 * @param microsecond 微秒数
 * @return QTime对象
 */
QTime microsecondToTime(const quint64& microsecond)
{
     quint64 timeCount=microsecond;
     int h=timeCount/1000/1000/60/60;
     timeCount=timeCount%((quint64)1000*1000*60*60);
     int m=timeCount/1000/1000/60;
     timeCount=timeCount%((quint64)1000*1000*60);
     int s=timeCount/1000/1000;
     timeCount=timeCount%((quint64)1000*1000);
     int ms=timeCount/1000;
     return QTime(h,m,s,ms);
}
GY_PlayBackControlWidget::GY_PlayBackControlWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GY_PlayBackControlWidget),
    m_PlayBackControl(PlayBackControl::QtTimer,this),
    m_FindTimeFlag(false)
{
    ui->setupUi(this);
    connect(&m_PlayBackControl,&PlayBackControl::sendPlayBackDataSignal,this,&GY_PlayBackControlWidget::sendPlayBackDataSignal);
    m_CurrentTimeUpdataTimer.setInterval(300);
    connect(&m_CurrentTimeUpdataTimer,&QTimer::timeout,this,&GY_PlayBackControlWidget::currentTimeUpdate);
    m_PlaySpeed=1.0f;
    m_PlayBackState=PlayBackState::PlayBackStop;
}

GY_PlayBackControlWidget::~GY_PlayBackControlWidget()
{
    delete ui;
}
//暂停
void GY_PlayBackControlWidget::on_pushButton_Pause_clicked()
{
    if(m_PlayBackState==PlayBackState::PlayBackStop||m_PlayBackState==PlayBackState::PlayBackPause||pausePlayBackSignal())
    {
        setPlayBackState();
        return;
    }
    m_PlayBackControl.pausePlaying();
    m_CurrentTimeUpdataTimer.stop();
    m_PlayBackState=PlayBackState::PlayBackPause;
}
//停止
void GY_PlayBackControlWidget::on_pushButton_Stop_clicked()
{
    if(m_PlayBackState==PlayBackState::PlayBackStop||stopPlayBackSignal())
    {
        setPlayBackState();
        return;
    }
    m_PlayBackControl.stopPlaying();
    m_CurrentTimeUpdataTimer.stop();
    m_PlayBackState=PlayBackState::PlayBackStop;
}
//播放
void GY_PlayBackControlWidget::on_pushButton_Play_clicked()
{
    if(m_PlayBackState!=PlayBackState::PlayBacking)
    {
        if(startPlayBackSignal())
        {
            setPlayBackState();
            return;
        }
        m_PlayBackControl.startPlayBack();
        m_PlayBackState=PlayBackState::PlayBacking;
        m_CurrentTimeUpdataTimer.start();
    }
    else if(m_PlayBackState==PlayBackState::PlayBacking&&m_PlaySpeed!=1)
    {
         m_PlaySpeed=1;
         m_PlayBackControl.setPlaySpeed(m_PlaySpeed);
    }
    else
    {
        m_PlayBackControl.pausePlaying();
        m_PlayBackState=PlayBackState::PlayBackPause;
        m_CurrentTimeUpdataTimer.stop();
    }
    setPlayBackState();
}
//快进
void GY_PlayBackControlWidget::on_pushButton_FastForward_clicked()
{
    if(m_PlayBackState!=PlayBackState::PlayBacking||m_PlaySpeed>=MAX_PLAY_FORWARD_SPEED)
    {
        setPlayBackState();
        return;
    }
    m_PlaySpeed=m_PlaySpeed<1?2:m_PlaySpeed*2;
    m_PlayBackControl.setPlaySpeed(m_PlaySpeed);
    ui->label_PlayState->setText(QString("快进X%1").arg(m_PlaySpeed));
}
//快退
void GY_PlayBackControlWidget::on_pushButton_FastBackward_clicked()
{
    if(m_PlayBackState!=PlayBackState::PlayBacking||m_PlaySpeed<=MAX_PLAY_BACKWARD_SPEED)
    {
        setPlayBackState();
        return;
    }
    m_PlaySpeed=m_PlaySpeed>0?-1:m_PlaySpeed*2;
    m_PlayBackControl.setPlaySpeed(m_PlaySpeed);
    ui->label_PlayState->setText(QString("快退X%1").arg(qAbs(m_PlaySpeed)));
}
//更新当前时间
void GY_PlayBackControlWidget::currentTimeUpdate()
{
   quint64 time=m_PlayBackControl.currentTime();
   double aa=((double)time-m_PlayBackControl.startTime())/(m_PlayBackControl.endTime()-m_PlayBackControl.startTime())*ui->horizontalSlider_Time->maximum();
   ui->horizontalSlider_Time->setValue(((double)time-m_PlayBackControl.startTime())/(m_PlayBackControl.endTime()-m_PlayBackControl.startTime())*ui->horizontalSlider_Time->maximum());
   ui->label_CurrentTime->setText(microsecondToString(time));
}
/**
 * @brief setStartEndDateTime 设置开始、结束的日期和时间
 * @param startTime 开始时间
 * @param endTime 结束时间
 * @param startDate 开始日期：默认2000-1-1
 * @param endDate 结束日期：默认2000-1-1
 */
void GY_PlayBackControlWidget::setStartEndDateTime(const quint64 &startTime, const quint64 &endTime, const QDate &startDate, const QDate &endDate)
{
    if(m_PlayBackState==PlayBackState::PlayBacking)
    {
        return;
    }
    ui->label_StartTime->setText(microsecondToString(startTime));
    ui->label_EndTime->setText(microsecondToString(endTime));
    m_PlayBackControl.setStartEndDateTime(startTime,endTime,startDate,endDate);
    ui->timeEdit_GoTime->setDateTime(QDateTime(startDate,microsecondToTime(startTime)));
}
/**
 * @brief addPlayBackData 添加回放数据
 * @param date 日期
 * @param time 时间：us
 * @param msgId 消息id
 * @param data 数据
 */
void GY_PlayBackControlWidget::addPlayBackData(const QDate &date, const quint64 &time, const QVariant &msgId, const QVariant &data)
{
    m_PlayBackControl.addPlayBackData(date,time,msgId,data);
}
/**
 * @brief setPlayBackState 根据播放状态和速度设置播放状态显示
 */
void GY_PlayBackControlWidget::setPlayBackState()
{
    switch(m_PlayBackState)
    {
        case PlayBackState::PlayBacking:
        {
            if(m_PlaySpeed==1)
            {
                ui->pushButton_Play->setChecked(true);
                ui->label_PlayState->setText("正常播放X1");
            }
            else if(m_PlaySpeed<0)
            {
                ui->pushButton_FastBackward->setChecked(true);
                ui->label_PlayState->setText(QString("快退X%1").arg(m_PlaySpeed));
            }
            else
            {
                ui->pushButton_FastForward->setChecked(true);
                ui->label_PlayState->setText(QString("快进X%1").arg(qAbs(m_PlaySpeed)));
            }
        }break;
        case PlayBackState::PlayBackPause:
        {
            ui->pushButton_Pause->setChecked(true);
            ui->label_PlayState->setText("暂停中");
        }break;
        case PlayBackState::PlayBackStop:
        {
            ui->pushButton_Stop->setChecked(true);
            ui->label_PlayState->setText("已停止");
        }break;
    }
}
/**
 * @brief on_pushButton_GoTime_clicked  时间跳转
 */
void GY_PlayBackControlWidget::on_pushButton_GoTime_clicked()
{
    if(m_PlayBackState!=PlayBackState::PlayBacking
            ||ui->timeEdit_GoTime->date()<m_PlayBackControl.startDate()
            ||ui->timeEdit_GoTime->date()>m_PlayBackControl.endDate()
            ||ui->timeEdit_GoTime->time().msec()*1000>m_PlayBackControl.endTime()
            ||ui->timeEdit_GoTime->time().msec()*1000<m_PlayBackControl.startTime())
    {
        return;
    }
    on_pushButton_Pause_clicked();
    if(goToTime(ui->timeEdit_GoTime->date(),((quint64)QTime(0,0).msecsTo(ui->timeEdit_GoTime->time()))*1000))
    {
        m_PlayBackControl.clear();
    }
    on_pushButton_Play_clicked();
}

/**
 * @brief on_horizontalSlider_Time_sliderReleased  时间滑块抬起
 */
void GY_PlayBackControlWidget::on_horizontalSlider_Time_sliderReleased()
{
    if(!m_FindTimeFlag)
    {
        return;
    }
    QDateTime statrDateTime;
    statrDateTime.setDate(m_PlayBackControl.startDate());
    statrDateTime=statrDateTime.addMSecs(m_PlayBackControl.startTime()/1000);
    QDateTime endDateTime;
    endDateTime.setDate(m_PlayBackControl.endDate());
    endDateTime=endDateTime.addMSecs(m_PlayBackControl.endTime()/1000);
    QDateTime goToDateTime=statrDateTime.addMSecs(statrDateTime.msecsTo(endDateTime)*((double)ui->horizontalSlider_Time->value()/ui->horizontalSlider_Time->maximum()));
    bool isFind=goToTime(goToDateTime.date(),((quint64)QTime(0,0).msecsTo(goToDateTime.time()))*1000);
    if(isFind)
    {
        m_PlayBackControl.clear();
        m_PlayBackControl.setCurrentDateTime(((quint64)QTime(0,0).msecsTo(goToDateTime.time()))*1000,goToDateTime.date());
    }
    on_pushButton_Play_clicked();
}

void GY_PlayBackControlWidget::on_horizontalSlider_Time_sliderPressed()
{
    if(m_PlayBackState!=PlayBackState::PlayBacking)
    {
        ui->horizontalSlider_Time->setValue(0);
        return;
    }
    m_FindTimeFlag=true;
    on_pushButton_Pause_clicked();
}
